package com.jingan.nursinghome.regulatory.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.MainActivity;
import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog2.RichangDialog2;
import com.jingan.nursinghome.regulatory.dialog2.XuantiMenuDialog;
import com.jingan.nursinghome.regulatory.dialog2.XuantiMenuDialog2;
import com.jingan.nursinghome.regulatory.model.Topic;
import com.jingan.nursinghome.regulatory.view.AddView;
import com.jingan.nursinghome.regulatory.view.CircleView;

import java.util.List;

import cn.android.support.v7.lib.sin.crown.dialog.AlertDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/24.
 */

public class RichangAdapter extends RecyclerView.Adapter<RichangAdapter.MyViewHoldView> {

    List<String> list;

    public RichangAdapter(List<String> list) {
        this.list = list;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }


    public class MyViewHoldView extends RecyclerView.ViewHolder {
        TextView info;
        TextView del;

        public MyViewHoldView(View itemView) {
            super(itemView);
            ProportionUtils.getInstance().adapterView(itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout));
            info = (TextView) itemView.findViewById(R.id.info);
            del = (TextView) itemView.findViewById(R.id.del);
            ProportionUtils.getInstance().adapterTextView(info);
            ProportionUtils.getInstance().adapterTextView(del);
            ProportionUtils.getInstance().adapterView(itemView.findViewById(R.id.line));
        }
    }


    @Override
    public MyViewHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_richang2, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
        if (list == null || viewType == list.size()) {
            return new MyViewHoldView(new AddView(parent.getContext()));
        }
        return new MyViewHoldView(view);
        //return null;
    }

    @Override
    public void onBindViewHolder(MyViewHoldView holder, final int position) {
        if (list == null || position == list.size()) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    XuantiMenuDialog.getInstance(RichangDialog2.richangDialog2.baseActivity);
                }
            });
        } else {
            holder.itemView.findViewById(R.id.line).setVisibility(View.VISIBLE);
            if (position == list.size() - 1) {
                holder.itemView.findViewById(R.id.line).setVisibility(View.INVISIBLE);
            }
            //Log.e("test","题号:\t"+MicrometerTable.getInstance().richangS.get(position));
            //MicrometerTable.getInstance().richangS.get(position)) - 1 之所以要减1，是因为存储的count是从1开始的。而下标是从0开始的。
            holder.info.setText((position + 1) + "." + MicrometerTable.getInstance().getTopicsAll().get(Integer.valueOf(MicrometerTable.getInstance().richangS.get(position)) - 1).getQuestion());
            holder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //删除
                    AlertDialog.getInstance(MainActivity.mainActivity, true, true).setMession("确认删除?").setPositive("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MicrometerTable.getInstance().jianRiChange(MicrometerTable.getInstance().richangS.get(position));
                            MicrometerTable.getInstance().saveRichangIndex();
                            list = MicrometerTable.getInstance().getRichangIndex();
                            //notifyDataSetChanged();//这个更新不行。
                            RichangDialog2.richangDialog2.RAapter();//这个才行。
                            AlertDialog.dismisss();
                        }
                    });
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        //return super.getItemViewType(position);
        return position;
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size() + 1;
        }
        return 1;
    }
}
